﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AAA.Meta.Chart.Data;

namespace AAA.Chart.Component
{
    public partial class PChartContainer : UserControl
    {
        private ProfileMgr _ProfileMgr;
        private float _fBoxHeight = 14;
        private float _fBoxWidth = 14;
        private float _fScale = 1;
        private float _fXStartIndex = 0;
        private float _fYStartIndex = 0;
        private float _fXEndIndex = 0;
        private float _fYEndIndex = 0;

        public PChartContainer() : this(new ProfileMgr())
        {
        }

        public PChartContainer(ProfileMgr profileMgr)
        {
            InitializeComponent();

            try
            {
                _ProfileMgr = profileMgr;
                _fScale = 1;
                _fXStartIndex = 0;
                _fYStartIndex = 0;
                _fXEndIndex = 0;
                _fYEndIndex = 0;
                profileVTChart1.ChartType = 0;
                profileVTChart2.ChartType = 1;
                splitContainer1.SplitterDistance = (int)(splitContainer1.Width * 0.7);
                splitContainer2.SplitterDistance = (int)(splitContainer2.Width * 0.5);
                hScrollBar.ValueChanged += new EventHandler(OnValueChanged);
                vScrollBar.ValueChanged += new EventHandler(OnValueChanged);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message.ToString());
            }
        }

        public int SelectMode
        {
            get { return profileMChart1.SelectMode; }
            set { profileMChart1.SelectMode = value; }
        }

        public float SetScale
        {
            get { return _fScale; }
            set { _fScale = value; }
        }
        
        void OnValueChanged(object sender, EventArgs e)
        {
            try 
            {
                YStartIndex = Math.Max(0, vScrollBar.Value);
                XStartIndex = Math.Max(0, hScrollBar.Value);
                RedrawChart();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

        private int CalculateYEndIndex()
        {
            return (int)Math.Min(YVisibleCount() + _fYStartIndex, profileMChart1.PriceCount);
        }

        private int CalculateXEndIndex()
        {
            return (int)Math.Min(XVisibleCount() + _fXStartIndex, _ProfileMgr.POCTPOCount() + 2 * (_ProfileMgr.GetTotalProfileCount() - 1));
        }

        public int YStartIndex
        {
            get { return (int)_fYStartIndex; }
            set
            {
                _fYStartIndex = value;
                _fYEndIndex = CalculateYEndIndex();
                profileMChart1.YStartIndex = value;
                profileMChart1.YEndIndex = _fYEndIndex;
                profileVTChart2.YStartIndex = value;
                profileVTChart2.YEndIndex = _fYEndIndex;
                profileVTChart1.YStartIndex = value;
                profileVTChart1.YEndIndex = _fYEndIndex;
            }
        }

        public int XStartIndex
        {
            get { return (int)_fXStartIndex; }
            set
            {
                _fXStartIndex = value;
                _fXEndIndex = CalculateXEndIndex();
                profileMChart1.XStartIndex = value;
                profileMChart1.XEndIndex = _fXEndIndex;
            }
        }

        private void ResetScrollRange()
        {
            try 
            {
                hScrollBar.Maximum = (int)Math.Max(0, _ProfileMgr.POCTPOCount() + 2 * (_ProfileMgr.GetTotalProfileCount() - 1) - XVisibleCount()); 
                vScrollBar.Maximum = (int)Math.Max(0, profileMChart1.PriceCount + 2 - YVisibleCount()); 
                hScrollBar.Minimum = 0;
                vScrollBar.Minimum = 0;
                if (hScrollBar.Maximum == 0)  {
                    hScrollBar.Visible = false;
                }
                else {
                    hScrollBar.Visible = true;
                }
                if (vScrollBar.Maximum == 0)
                {
                    vScrollBar.Visible = false;
                }
                else
                {
                    vScrollBar.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

        public void RedrawChart()
        {
            try
            {
                ResetScrollRange();
                YStartIndex = Math.Max(0, vScrollBar.Value);
                XStartIndex = Math.Max(0, hScrollBar.Value);

                profileMChart1.PaintChart();
                profileVTChart2.PaintChart();
                profileVTChart1.PaintChart();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

        public void AddProfileMgr(ProfileMgr InputProfileMgr) {
            try {
                _ProfileMgr = InputProfileMgr;
                profileMChart1.AddProfileMgr(_ProfileMgr);
                profileVTChart2.ShowYAxis = !profileMChart1.Visible;
                //profileVTChart2.AddProfileMgr(_ProfileMgr);
                profileVTChart2.ResetRange(_ProfileMgr);
                profileVTChart1.ShowYAxis = !profileMChart1.Visible;
                //profileVTChart1.AddProfileMgr(_ProfileMgr);
                profileVTChart1.ResetRange(_ProfileMgr);
                RedrawChart();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

        private void PChartContainer_Resize(object sender, EventArgs e)
        {
            try
            {
                hScrollBar.Left = vScrollBar.Width;
                hScrollBar.Top = Height - hScrollBar.Height;
                hScrollBar.Width = Width - vScrollBar.Width;
                hScrollBar.Visible = true;
                vScrollBar.Left = 0;
                vScrollBar.Top = 0;
                vScrollBar.Height = Height - hScrollBar.Height;
                vScrollBar.Visible = true;
                splitContainer1.Left = vScrollBar.Width;
                splitContainer1.Top = 0;
                splitContainer1.Height = Height - hScrollBar.Height;
                splitContainer1.Width = Width - vScrollBar.Width;
                splitContainer1.Visible = true;
                RedrawChart();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

        private int YVisibleCount() {
            try {
                return (int)Math.Ceiling((profileMChart1.Height - profileMChart1.AxisXAreaHeight) / (_fBoxHeight * _fScale));
            }
            catch (Exception ex){
                Console.WriteLine(ex.Message.ToString());
            }
            return 0;
        }

        private int XVisibleCount()
        {
            try
            {
                return (int)Math.Ceiling((profileMChart1.Width - profileMChart1.AxisYBoxWidth - 4) / (_fBoxWidth * _fScale));
            }
            catch (Exception ex){
                Console.WriteLine(ex.Message.ToString());
            }
            return 0;
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            ChangeSize();
        }

        private void ChangeSize() {
            hScrollBar.Width = profileMChart1.Width;
            ResetScrollRange();
            //RedrawChart();
        }

        private void splitContainer1_SizeChanged(object sender, EventArgs e)
        {
            ChangeSize();
        }

        public void SetLastPos() {
            hScrollBar.Value = hScrollBar.Maximum;
            SetLastPriceCenter();
        }

        public void SetLastPriceCenter() {
            try
            {
                float LastIndex = profileMChart1.GetLastPriceIndex();
                int iHalfVisible = (int)Math.Ceiling((double)YVisibleCount() / 2) + 2;
                float fStart = LastIndex - iHalfVisible;
                float fEnd = LastIndex + iHalfVisible;
                if (fEnd > (profileMChart1.PriceCount))
                {
                    fEnd = profileMChart1.PriceCount;
                    fStart = fEnd - YVisibleCount();
                }
                vScrollBar.Value = Math.Max(0,(int)fEnd - YVisibleCount() + 2);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message.ToString());
            }
        }

        public void SetLastDayProfileVT() { 
            profileVTChart1.SetLastDayProfile();
            profileVTChart2.SetLastDayProfile();
        }

        private void btnZoomOut_Click(object sender, EventArgs e)
        {
            btnZoomInOut(-2);
        }

        private void btnZoomIn_Click(object sender, EventArgs e)
        {
            btnZoomInOut(2);
        }

        private void btnZoomInOut(int intChange)
        {
            if ((_fBoxHeight >= 4) || (_fBoxHeight <= 20))
            {
                _fBoxHeight = _fBoxHeight + intChange;
                _fBoxWidth = _fBoxWidth + intChange;
                profileMChart1.Chart_ZoomInOut(intChange);
                profileVTChart1.Chart_ZoomInOut(intChange);
                profileVTChart2.Chart_ZoomInOut(intChange);
                RedrawChart();
            }
        }

        public int GetVTChartMode()
        {
            return profileVTChart1.ChartMode;
        }
    }
}
